require './date.coffee'
module.exports = angular.module 'libs.utils.common', [
  require('constant').name
]
.factory 'CommonService', ($log, Constant)->
  $log.log 'common service init'
  class CommonService
    constructor: ->
      @constant = new Constant()

    upload: (options)->
      defaultOptions =
        runtimes:         "html5,flash,html4"
        browse_button:    "upload_btn"
        uptoken_url:      @constant.QI_NIU_TOKEN_URL
        domain:           @constant.QI_NIU_URL
        get_new_uptoken:  false
        container:        "upload_container"
        max_file_size:    "10mb"
        max_retries:      3
        dragdrop:         true
        drop_element:     "upload_container"
        chunk_size:       "4mb"
        auto_start:       true
        unique_names:     true
        init:
          "FilesAdded": (up, files)->
          "BeforeUpload": (up, files)->
          "UploadProgress": (up, files)->
          "FileUploaded": (up, files, info)->
          "Error": (up, err, errTip)->
          "UploadComplete": ()->
          "Key": (up, file)->
      uploadOptions = {}
      angular.extend uploadOptions, defaultOptions, options
      Qiniu.uploader uploadOptions

    copyExcludeEmptyString: (src, dest)->
      return if !src or !dest
      dest[key] = value for key, value of src when value isnt ""

    endWithFirstIndexString: (str)->
      length = str.length
      url = $location.url()
      suffix = url.substring( url.indexOf( str ) + length )
      return true if suffix is "/" or suffix is ""
      return false

    defaultGo: (indexString, state, params)->
      $state.go state, params if @endWithFirstIndexString( indexString )

    imageRepackage: (images, cols, excludeStatus)->
      result = []
      temp = []
      imageList = []
      if excludeStatus
        for image in images
          imageList.push image if image.status isnt excludeStatus
      else
        imageList = images

      for image, i in imageList
        if (i % cols) is 0
          result.push temp if temp.length isnt 0
          temp = []
        temp.push image
      result.push temp if temp.length isnt 0
      return result

    currencyToFloat: (currencyString, currencySymbol)->
      return 0 if typeof currencyString isnt "string"
      reg = eval "/(\\" + currencySymbol + "|\,)+/g"
      parseFloat currencyString.replace(reg , '')


    getUrlParam: (url, paramKey)->
      return null if !url?
      return null if (index = url.indexOf "?") is -1
      params = url.substring index + 1
      return params if !paramKey?

      params = params.split "&"
      return param.split("=")[1] for param in params when param.indexOf("=") isnt -1 && param.split("=")[0] is paramKey

    getDayBegin: (date)->
      try
        new Date(date + " 00:00:00").getTime()
      catch err
        return ''
    getDayEnd: (date)->
      try
        new Date(date + " 23:59:59").getTime()
      catch err
        return ''
    getDay: (date)->
      d = new Date(date)
      d.Format('yyyy-MM-dd') if Object.prototype.toString.call(d) is "[object Date]" and !isNaN(d.getTime())
    isDate: (date)->
      d = new Date(date)
      Object.prototype.toString.call(d) is "[object Date]" and !isNaN(d.getTime())

